﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Module_Ribbon_Bar_Item_Detail : DetailPageBase
{
    protected void Page_Load(object sender, EventArgs e)
    {
        base.initializeWebForm();

        if (!IsPostBack)
        {
            Genesis.Client.Common.Classes.LookupProvider lookupProvider = new Genesis.Client.Common.Classes.LookupProvider();

            System.Data.DataTable barItemTypeTable = lookupProvider.GetLookupValues("Lookup_BarItemType", "BarItemTypeId", "BarItemTypeName", "GenesisConnectionString");

            barItemTypeDropDownList.DataSource = barItemTypeTable;
            barItemTypeDropDownList.DataTextField = "Display";
            barItemTypeDropDownList.DataValueField = "Value";

            barItemTypeDropDownList.DataBind();

            Genesis.Client.Module.Classes.ModuleProvider moduleProvider = new Genesis.Client.Module.Classes.ModuleProvider();

            System.Data.DataTable barItemTable = moduleProvider.BarItemGetListWithNullRecord();

            parentBarItemDropDownList.DataSource = barItemTable;
            parentBarItemDropDownList.DataTextField = "BarItemName";
            parentBarItemDropDownList.DataValueField = "BarItemGuid";

            parentBarItemDropDownList.DataBind();

            System.Data.DataTable ribbonBarTable = moduleProvider.RibbonBarGetList();

            ribbonBarDropDownList.DataSource = ribbonBarTable;
            ribbonBarDropDownList.DataTextField = "RibbonBarName";
            ribbonBarDropDownList.DataValueField = "RibbonBarGuid";

            ribbonBarDropDownList.DataBind();

            System.Data.DataTable commandTable = moduleProvider.CommandGetListWithNullRecord();

            commandDropDownList.DataSource = commandTable;
            commandDropDownList.DataTextField = "CommandName";
            commandDropDownList.DataValueField = "CommandGuid";

            commandDropDownList.DataBind();

            System.Data.DataTable fileTypeTable = moduleProvider.FileGetByFileTypeId(System.Convert.ToInt32(System.Configuration.ConfigurationManager.AppSettings["BarItemImageFileTypeId"]));

            barItemImageDropDownList.DataSource = fileTypeTable;
            barItemImageDropDownList.DataTextField = "FileUrl";
            barItemImageDropDownList.DataValueField = "FileGuid";

            barItemImageDropDownList.DataBind();

            Guid _barItemGuid = Guid.Empty;

            if (Request.QueryString["BarItemGuid"] != null)
            {
                _barItemGuid = new Guid(Request.QueryString["BarItemGuid"]);
            }

            if (_barItemGuid != Guid.Empty)
            {
                System.Data.DataTable roleTable = moduleProvider.BarItemGetByBarItemGuid(_barItemGuid);

                if (roleTable.Rows.Count == 1)
                {
                    System.Data.DataRow roleRow = roleTable.Rows[0];

                    barItemIdTextBox.Text = System.Convert.ToString(roleRow["BarItemId"]);
                    barItemGuidTextBox.Text = System.Convert.ToString(roleRow["BarItemGuid"]);
                    orderTextBox.Text = System.Convert.ToString(roleRow["BarItemOrder"]);

                    barItemNameTextBox.Text = System.Convert.ToString(roleRow["BarItemName"]);
                    barItemImageTextBox.Text = System.Convert.ToString(roleRow["BarItemImage"]);
                    barItemImageSizeTextBox.Text = System.Convert.ToString(roleRow["BarItemImageSize"]);

                    foreach (ListItem barItemImageItem in barItemImageDropDownList.Items)
                    {
                        if (barItemImageItem.Text.EndsWith(barItemImageTextBox.Text))
                        {
                            barItemImageItem.Selected = true;
                            break;
                        }
                    }

                    string _parentBarItemGuidString = System.Convert.ToString(roleRow["ParentBarItemGuid"]);

                    foreach (ListItem parentBarItemItem in parentBarItemDropDownList.Items)
                    {
                        if (parentBarItemItem.Value == _parentBarItemGuidString)
                        {
                            parentBarItemItem.Selected = true;
                            break;
                        }
                    }

                    string _ribbonBarGuidString = System.Convert.ToString(roleRow["RibbonBarGuid"]);

                    foreach (ListItem ribbonBarItem in ribbonBarDropDownList.Items)
                    {
                        if (ribbonBarItem.Value == _ribbonBarGuidString)
                        {
                            ribbonBarItem.Selected = true;
                            break;
                        }
                    }

                    string _barItemTypeIdString = System.Convert.ToString(roleRow["BarItemTypeId"]);

                    foreach (ListItem barItemTypeItem in barItemTypeDropDownList.Items)
                    {
                        if (barItemTypeItem.Value == _barItemTypeIdString)
                        {
                            barItemTypeItem.Selected = true;
                            break;
                        }
                    }

                    string _commandGuidString = System.Convert.ToString(roleRow["CommandGuid"]);

                    foreach (ListItem commandItem in commandDropDownList.Items)
                    {
                        if (commandItem.Value == _commandGuidString)
                        {
                            commandItem.Selected = true;
                            break;
                        }
                    }
                }
            }

            switch (formMode)
            {
                case FormMode.Create:
                    barItemNameTextBox.Enabled = true;
                    orderTextBox.Enabled = true;

                    barItemNameTextBox.ReadOnly = false;
                    orderTextBox.ReadOnly = false;

                    deleteImageButton.Visible = false;
                    break;
                case FormMode.Delete:
                    barItemNameTextBox.Enabled = true;
                    orderTextBox.Enabled = true;

                    barItemNameTextBox.ReadOnly = true;
                    orderTextBox.ReadOnly = true;

                    deleteImageButton.Visible = true;
                    break;
                case FormMode.Edit:
                    barItemNameTextBox.Enabled = true;
                    orderTextBox.Enabled = true;

                    barItemNameTextBox.ReadOnly = false;
                    orderTextBox.ReadOnly = false;

                    deleteImageButton.Visible = false;
                    break;
                case FormMode.Default:
                    barItemNameTextBox.Enabled = true;
                    orderTextBox.Enabled = true;

                    barItemNameTextBox.ReadOnly = true;
                    orderTextBox.ReadOnly = true;

                    deleteImageButton.Visible = false;
                    break;
            }
        }
    }
    protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
    {
        Response.Redirect(this.buildForwardUrl("Default.aspx"));
    }
    protected void ImageButton2_Click(object sender, ImageClickEventArgs e)
    {
        Genesis.Client.Module.Classes.ModuleProvider moduleProvider = new Genesis.Client.Module.Classes.ModuleProvider();

        if (formMode == FormMode.Edit)
        {
            if (!moduleProvider.BarItemUpdate(
                System.Convert.ToInt32(barItemIdTextBox.Text),
                new Guid(barItemGuidTextBox.Text),
                new Guid(ribbonBarDropDownList.SelectedValue),
                new Guid(parentBarItemDropDownList.SelectedValue),
                new Guid(commandDropDownList.SelectedValue),
                barItemNameTextBox.Text,
                barItemImageTextBox.Text,
                System.Convert.ToInt32(barItemImageSizeTextBox.Text),
                System.Convert.ToInt32(barItemTypeDropDownList.SelectedValue),
                System.Convert.ToInt32(orderTextBox.Text)))
            {
                Response.Write("alert(\"An error occured while updating the database\");");
            }
            else
            {
                Response.Redirect(this.buildForwardUrl("Default.aspx"));
            }
        }
        else if (formMode == FormMode.Create)
        {
            if (moduleProvider.BarItemInsert(
                Guid.NewGuid(),
                new Guid(ribbonBarDropDownList.SelectedValue),
                new Guid(parentBarItemDropDownList.SelectedValue),
                new Guid(commandDropDownList.SelectedValue),
                barItemNameTextBox.Text,
                barItemImageTextBox.Text,
                System.Convert.ToInt32(barItemImageSizeTextBox.Text),
                System.Convert.ToInt32(barItemTypeDropDownList.SelectedValue),
                System.Convert.ToInt32(orderTextBox.Text)) == 0)
            {
                Response.Write("alert(\"An error occured while creating database entry\");");
            }
            else
            {
                Response.Redirect(this.buildForwardUrl("Default.aspx"));
            }
        }
    }
    protected void barItemImageDropDownList_SelectedIndexChanged(object sender, EventArgs e)
    {
        barItemImageTextBox.Text = barItemImageDropDownList.SelectedItem.Text.Substring(barItemImageDropDownList.SelectedItem.Text.LastIndexOf("/") + 1);
    }
}
